package com.feng.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class ScopeController {
    @RequestMapping("/servletAPI")
    public String TestServletAPI(HttpServletRequest request){
        request.setAttribute("servletapi","Hello ServletAPI");
        return "success";
    }

    @RequestMapping("/modelAndView")
    public ModelAndView TestModelAndView(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("modelAndView","Hello modelAndView");
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping("/model")
    public String TestModel(Model model){
        model.addAttribute("model","Hello model");
        return "success";
    }

    @RequestMapping("/map")
    public String TestMap(Map<String,String> map){
        map.put("map","Hello map");
        return "success";
    }

    @RequestMapping("/modelMap")
    public String TestModelMap(ModelMap modelMap){
        modelMap.addAttribute("modelMap","Hello modelMap");
        return "success";
    }

    @RequestMapping("/testSession")
    public String TestSession(HttpSession session){
        session.setAttribute("sessioner","Hello Session");
        return "success";
    }

    @RequestMapping("/testApplication")
    public String TestApplication(HttpSession session){
        ServletContext servletContext = session.getServletContext();
        servletContext.setAttribute("servletContext","Hello application");
        return "success";
    }
}
